package com.example.parkdemo.visitor

import android.content.DialogInterface
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.net.Const
import com.example.base.user.VisitorBean
import com.example.parkdemo.R
import com.example.parkdemo.adapter.VisitorAdapter
import com.example.parkdemo.databinding.ActivityVisitorBinding
import com.google.gson.Gson
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

/**
 * 来访
 */
@AndroidEntryPoint
class VisitorActivity : BaseActivity<ActivityVisitorBinding, VisitorViewModel>() {
    lateinit var visitorAdapter: VisitorAdapter
    var visitorId = 0
    var index = 0
    override fun initData() {
        vm.visitorSuccess.observe(this) {

            val list = Gson().fromJson(it, VisitorBean::class.java)
            visitorAdapter.data.clear()
            visitorAdapter.data.addAll(list)
            visitorAdapter.notifyDataSetChanged()
        }
        vm.deleteSuccess.observe(this) {
            ToastUtils.showLong("删除成功")
            visitorAdapter.removeAt(index)
        }
    }

    override fun onResume() {
        super.onResume()
        val map = mutableMapOf<String, Int>()
        map[Const.PARAM_SIGN_USER_ID] = 0
        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        vm.getVisitor(body)
    }

    override fun initView() {
        binding.visitorHeader.headerBack.setOnClickListener { finish() }
        binding.visitorHeader.headerTitle.text = "来访预约"
        binding.visitorHeader.headerAdd.visibility = View.VISIBLE
        binding.visitorHeader.headerAdd.setOnClickListener {
            startActivity(Intent(this, AddVisitorActivity::class.java))
        }
        visitorAdapter = VisitorAdapter()
        binding.visitorRv.adapter = visitorAdapter
        binding.visitorRv.layoutManager = LinearLayoutManager(this)

        visitorAdapter.addChildClickViewIds(R.id.item_visitor_delete)
        visitorAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id) {
                R.id.item_visitor_delete -> {
                    AlertDialog.Builder(this)
                        .setTitle("是否删除")
                        .setPositiveButton("确定", object : DialogInterface.OnClickListener {
                            override fun onClick(dialog: DialogInterface?, which: Int) {
                                visitorId = visitorAdapter.data[position].visitorId
                                index = position
                                val map = mutableMapOf<String, Int>()
                                map[Const.PARAM_VISITOR_ID] = visitorId
                                val json = Gson().toJson(map)
                                val body =
                                    RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
                                vm.getDeleteSuccess(body)

                            }

                        })
                        .setNegativeButton("取消", null)
                        .show()
                }
            }
        }
    }

}